//
//  CIBNScrollMenuView.m
//  CIBN
//
//  Created by ZNGOO on 16/7/18.
//  Copyright © 2016年 韦儒健. All rights reserved.
//

#import "CIBNScrollMenuView.h"
#import "CIBNMenuListModel.h"

@interface CIBNScrollMenuView() <UIScrollViewDelegate>

/**  滚动视图 */
@property (nonatomic,strong) UIScrollView * scrollView;

/**  数组 */
@property (nonatomic,strong) NSArray * items;

/**  所有按钮 */
@property (nonatomic,strong) UIImageView * allBtn;


@end

@implementation CIBNScrollMenuView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width - 50, self.bounds.size.height)];
        self.scrollView.delegate = self;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.bounces = NO;
        self.scrollView.backgroundColor = RGB(255, 255, 255);
        [self addSubview:self.scrollView];
    }
    return self;
}

- (void)setItemsArray:(NSArray *)items{
    
    
    // 1. 创建ScrollView
    
    NSMutableArray * arr = [[NSMutableArray alloc] init];
    for (CIBNMenuListModel * model in items) {
        if (![model.Name isEqualToString:@"会员专区"]) {
            [arr addObject:model];
        }
   
    }
    self.items = arr;
    
    // 2. 计算ScrollView的最大宽度
    CGFloat W = 10.0;
    
    for (int index = 0; index < arr.count-1; index++) {
        
        CIBNMenuListModel * model = arr[index];
        
        CGFloat w = [ZJModelTool sizeWithText:model.Name font:[UIFont systemFontOfSize:14] height: self.bounds.size.height] + 10;
        
        // 1. 菜单
        UILabel * item = [[UILabel alloc] initWithFrame:CGRectMake(W, 0, w, self.bounds.size.height-1)];
        item.text = model.Name;
        item.textAlignment = NSTextAlignmentCenter;
        item.font = [UIFont systemFontOfSize:14];
        item.textColor = RGB(28, 32, 41);
        item.tag = index;
        item.userInteractionEnabled = YES;
        [self.scrollView addSubview:item];
        
        
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapItem:)];
        [item addGestureRecognizer:tap];
        
        
        // 2. 下划线
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(W, self.bounds.size.height-1, w, 1)];
        image.backgroundColor = RGB(18, 146, 228);
        image.tag = index + 10;
        [self.scrollView addSubview:image];
        if (index != 0) {
            image.hidden = YES;
        }
        
        W = W + w + 10 ;
    }
    
    
    self.scrollView.contentSize = CGSizeMake(W, self.bounds.size.height);

    
    
    // 3. allBtn
    self.allBtn = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width - 45, 0, 40, self.bounds.size.height)];
    self.allBtn.image = [UIImage imageNamed:@"all"];
    self.allBtn.tag = 100;
    self.allBtn.userInteractionEnabled = YES;
    [self addSubview:self.allBtn];
    
    UITapGestureRecognizer * taps = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapItem:)];
    [self.allBtn addGestureRecognizer:taps];

}
#pragma mark - 点击
- (void)tapItem:(UITapGestureRecognizer *)tap{
    
//    for (int index = 0; index < self.items.count; index++) {
//        UIImageView * image = (UIImageView *)[self viewWithTag:index + 10];
//        image.hidden = YES;
//    }
//    
//    [UIView animateWithDuration:0.2 animations:^{
//        UIImageView * image = (UIImageView *)[self viewWithTag:tap.view.tag + 10];
//        image.hidden = NO;
//    }];
    
    
    
    // 代理设置
    if([self.delegate respondsToSelector:@selector(didSelectItemIndex:)]){
        [self.delegate didSelectItemIndex:tap.view.tag];
    }
}


- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    NSLog(@"%f",scrollView.contentOffset.x);
}



@end
